#!/usr/bin/env python

import os
import sys
import subprocess

class Logging:
	# TODO maybe the right way to do this is to use something like colorama?
	RED = '\033[31m'
	GREEN = '\033[32m'
	YELLOW = '\033[33m'
	MAGENTA = '\033[35m'
	RESET = '\033[0m'

	@staticmethod
	def _print(s, color=None):
		if color and sys.stdout.isatty() and sys.platform != 'win32':
			print(color + s + Logging.RESET)
		else:
			print(s)

	@staticmethod
	def debug(s):
		Logging._print(s, Logging.MAGENTA)

	@staticmethod
	def info(s):
		Logging._print(s, Logging.GREEN)

	@staticmethod
	def warning(s):
		Logging._print(s, Logging.YELLOW)

	@staticmethod
	def error(s):
		Logging._print(s, Logging.RED)

WIDTH = 15

def yumEnv(modelName, root):
	Logging.info('=' * WIDTH + modelName+ '=' * WIDTH )

	cmd = 'sudo yum install %s' % modelName
	if not root: cmd = 'pip install %s' % modelName

	question = 'Would u like to yum install [%s] : (y/n) ' % modelName
	isY = raw_input(question)
	if isY in ['n','N']:
		sys.exit(1)
	else:
		ret = subprocess.call(cmd, shell = True)
		if ret != 0:
			Logging.error(str(ret))
			sys.exit(1)
		else:
			cmd_v = '%s --version' % modelName
			subprocess.call(cmd_v, shell = True)

def pipEnv(modelName, root):
	Logging.info('*' * WIDTH + modelName+ '*' * WIDTH )

	cmd = 'sudo pip install %s' % modelName
	if not root: cmd = 'pip install %s' % modelName

	question = 'Would u like to pip install [%s] : (y/n) ' % modelName
	isY = raw_input(question)
	if isY in ['n','N']:
		sys.exit(1)
	else:
		ret = subprocess.call(cmd, shell = True)
		if ret != 0:
			Logging.error(str(ret))
			sys.exit(1)
		else:
			cmd_v = '%s --version' % modelName
			subprocess.call(cmd_v, shell = True)

def createEnv():
	Logging.info('*' * WIDTH+ 'create venv dir'+ '*' * WIDTH)
	cmd = 'virtualenv venv'
	ret = subprocess.call(cmd, shell = True)
	if ret != 0:
		Logging.error(str(ret))
		sys.exit(1)
	# Logging.info('=' * WIDTH + 'activate venv'+ '=' * WIDTH )
	# cmd_a = '. venv/bin/activate'
	# subprocess.call(cmd_a, shell = True)
	Logging.warning("[. venv/bin/activate] ....run this commond manual operation")

def confUwsgi():
	Logging.info('*' * WIDTH + 'config uWsgi server.py'+ '*' * WIDTH)
	cmd = 'uwsgi -s /tmp/uwsgi.sock -w server:app'
	ret = subprocess.call(cmd, shell = True)
	if ret != 0:
		Logging.error(str(ret))
		sys.exit(1)


if __name__ == '__main__':
	Logging.info(os.name)
	Logging.info(os.getenv('PATH'))
	Logging.info(sys.platform)
	pipEnv('virtualenv',True)
	yumEnv('python-devel',True)
	pipEnv('uwsgi',True)
	createEnv()
	yumEnv('libjpeg',True)
	yumEnv('libjpeg-devel',True)
	confUwsgi()
	createEnv()
	# pipEnv('-r requirements.txt', False)





